package SelectionSort;
import LinerSearch.ArrayGenerator;
public class SelectionSort {
    /**
     * 选择排序，选择后边最小的，互换
     */
    private SelectionSort(){

    }
    public static <E extends Comparable<E>> void sort(E[] arr){
        for (int i=0;i<arr.length;i++){
            int minIndex = i;
            for (int j=i+1;j<arr.length;j++){
                if (arr[j].compareTo(arr[minIndex])<0)
                    minIndex = j;
            }
            swap(arr,i,minIndex);
        }
    }

    private static <E> void swap(E[] arr, int i, int minIndex) {
        E tmp = arr[i];
        arr[i] = arr[minIndex];
        arr[minIndex] = tmp;
    }

    public static void main(String[] args) {
        int[] dataSize = {10000,100000};
        for (int n : dataSize){
            Integer[] arr = ArrayGenerator.generateRandomArray(n,n);

            SortingHelper.sortTest("SelectionSort",arr);
        }

//        long startTime = System.nanoTime();
//        SelectionSort.sort(arr);
//        long endTime = System.nanoTime();
//
//        double time = (endTime-startTime)/1000000000.0;
//
//        for (int i:arr){
//            System.out.print(i+" ");
//        }
//        System.out.println();
//        System.out.println(time+"s");
//        Student[] students = {new Student("Alice",98),
//        new Student("Bob",100),
//        new Student("Chandler",77)};
//        SelectionSort.sort(students);
//        for (Student student:students){
//            System.out.print(student+" ");
//        }
    }
}
